package com.dkd.manage.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import cn.hutool.core.bean.BeanUtil;
import com.dkd.common.utils.DateUtils;
import com.dkd.common.utils.uuid.UUIDUtils;
import com.dkd.manage.domain.Channel;
import com.dkd.manage.domain.Node;
import com.dkd.manage.domain.VmType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.dkd.manage.mapper.VendingMachineMapper;
import com.dkd.manage.domain.VendingMachine;
import com.dkd.manage.service.IVendingMachineService;

import static com.dkd.common.constant.DkdConstants.VM_STATUS_NODEPLOY;

/**
 * 设备Service业务层处理
 * 
 * @author crocodilePole
 * @date 2025-02-23
 */
@Service
public class VendingMachineServiceImpl implements IVendingMachineService 
{
    @Autowired
    private VendingMachineMapper vendingMachineMapper;

    @Autowired
    private VmTypeServiceImpl vmTypeService;
    @Autowired
    private NodeServiceImpl nodeService;
    @Autowired
    private ChannelServiceImpl channelService;

    /**
     * 查询设备
     * 
     * @param id 设备主键
     * @return 设备
     */
    @Override
    public VendingMachine selectVendingMachineById(Long id)
    {
        return vendingMachineMapper.selectVendingMachineById(id);
    }

    /**
     * 查询设备列表
     * 
     * @param vendingMachine 设备
     * @return 设备
     */
    @Override
    public List<VendingMachine> selectVendingMachineList(VendingMachine vendingMachine)
    {
        return vendingMachineMapper.selectVendingMachineList(vendingMachine);
    }

    /**
     * 新增设备
     * 
     * @param vendingMachine 设备
     * @return 结果
     */
    @Override
    public int insertVendingMachine(VendingMachine vendingMachine)
    {
        //新增设备数据
        VmType vmType = vmTypeService.selectVmTypeById(vendingMachine.getVmTypeId());
        Node node = nodeService.selectNodeById(vendingMachine.getNodeId());
        vendingMachine.setInnerCode(UUIDUtils.getUUID());
        vendingMachine.setChannelMaxCapacity(vmType.getChannelMaxCapacity());
        BeanUtil.copyProperties(node,vendingMachine,"id");
        vendingMachine.setAddr(node.getAddress());
        vendingMachine.setVmStatus(VM_STATUS_NODEPLOY);
        vendingMachine.setCreateTime(DateUtils.getNowDate());
        vendingMachine.setUpdateTime(DateUtils.getNowDate());
        int result = vendingMachineMapper.insertVendingMachine(vendingMachine);
        //新增货道数据
        ArrayList<Channel> channelList = new ArrayList<>();


        for (int i = 1; i <= vmType.getVmRow(); i++) {
            for (int l = 1; l <= vmType.getVmCol(); l++) {
                Channel channel = new Channel();
                channel.setVmId(vendingMachine.getId());
                channel.setInnerCode(vendingMachine.getInnerCode());
                channel.setMaxCapacity(vmType.getChannelMaxCapacity());
                channel.setCreateTime(DateUtils.getNowDate());
                channel.setUpdateTime(DateUtils.getNowDate());
                channel.setChannelCode(i + "-" + l);

                channelList.add(channel);
            }
        }

        channelService.insertChannels(channelList);

        return result;
    }

    /**
     * 修改设备
     * 
     * @param vendingMachine 设备
     * @return 结果
     */
    @Override
    public int updateVendingMachine(VendingMachine vendingMachine)
    {

        Node node = nodeService.selectNodeById(vendingMachine.getNodeId());
//        检查节点是否为空
        Optional.ofNullable(node).ifPresent(n->{
            BeanUtil.copyProperties(node,vendingMachine,"id");
            vendingMachine.setAddr(node.getAddress());
        });

        vendingMachine.setUpdateTime(DateUtils.getNowDate());
        return vendingMachineMapper.updateVendingMachine(vendingMachine);
    }

    /**
     * 批量删除设备
     * 
     * @param ids 需要删除的设备主键
     * @return 结果
     */
    @Override
    public int deleteVendingMachineByIds(Long[] ids)
    {
        return vendingMachineMapper.deleteVendingMachineByIds(ids);
    }

    /**
     * 删除设备信息
     * 
     * @param id 设备主键
     * @return 结果
     */
    @Override
    public int deleteVendingMachineById(Long id)
    {
        return vendingMachineMapper.deleteVendingMachineById(id);
    }

    /**
     * 查询设备列表
     *
     * @param innerCode 设备编号
     * @return VendingMachine
     */
    @Override
    public VendingMachine selectVendingMachineByInnerCode(String innerCode) {
        return vendingMachineMapper.selectVendingMachineByInnerCode(innerCode);
    }
}
